home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / svaerevert.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.3 KB  |  179 lines

  1. /*
  2.     File:        SVAERevert.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                 3.1 updates by Greg Sutton.
  8.  
  9.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #include "SVAERevert.h"
  25.  
  26. #include "SVEditWindow.h"        // for DPtrFromWindowPtr()
  27. #include "SVEditFile.h"
  28.  
  29. #include "SVEditAEUtils.h"
  30. #include "SVAETextUtils.h"
  31. #include "SVAEAccessors.h"
  32. #include "SVAEDelete.h"
  33.  
  34. #pragma segment File
  35.  
  36.  
  37. // -----------------------------------------------------------------------
  38. //    Name:         DoRevert
  39. //    Purpose:    Handles the Revert AppleEvent.
  40. // -----------------------------------------------------------------------
  41.  
  42. pascal OSErr DoRevert(const AppleEvent    *theAppleEvent,
  43.                             AppleEvent     *reply,
  44.                             long        handlerRefCon)
  45. #pragma unused (reply, handlerRefCon)
  46.  
  47.     AEDesc            directObj = {typeNull, NULL};
  48.     WindowToken        aWindowToken;
  49.     OSErr            err;
  50.     
  51.         // pick up the direct object
  52.     err = AEGetParamDesc(theAppleEvent,  keyDirectObject, typeWildCard, &directObj);
  53.     
  54.     err = GotRequiredParams(theAppleEvent);
  55.     if (noErr != err) goto done;
  56.     
  57.     if (typeNull != directObj.descriptorType)
  58.         err = RevertDesc(&directObj);
  59.     else if (FrontWindow())                        // If no direct object given, default
  60.     {                                            // to the front window.
  61.         aWindowToken.tokenWindow = FrontWindow();
  62.         err = RevertWindowToken(&aWindowToken);
  63.     }
  64.     else
  65.         err = errAENoSuchObject;
  66.         
  67. done:
  68.     if (directObj.dataHandle) 
  69.         AEDisposeDesc(&directObj);
  70.         
  71.     return(err);
  72. } // DoRevertWindow
  73.  
  74.  
  75. OSErr     RevertWindowToken(WindowToken* theToken)
  76. {
  77.     DPtr            docPtr;
  78.     TextToken        aTextToken;
  79.     OSErr            err;
  80.     
  81.     docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
  82.     
  83.     if (! docPtr)
  84.         return(errAENoSuchObject);
  85.  
  86.                         // Get a token for all the text in the window
  87.     err = TextTokenFromWindowToken(theToken, &aTextToken);
  88.     if (noErr != err) goto done;
  89.                         // Delete all the text
  90.     err = DeleteTextToken(&aTextToken);
  91.     if (noErr != err) goto done;
  92.  
  93.     if (docPtr->everSaved) 
  94.     {
  95.         err = GetFileContents(docPtr->theFSSpec, docPtr);
  96.         if (noErr != err) goto done;
  97.  
  98.         DoResizeWindow(docPtr);
  99.         docPtr->dirty = false;
  100.     }
  101.  
  102.     DoUpdate(docPtr->theWindow);
  103.  
  104. done:
  105.     return(err);
  106. }
  107.  
  108.  
  109. OSErr    RevertWindowDesc(AEDesc* windowDesc)
  110. {
  111.     WindowToken        aWindowToken;
  112.     Size            actualSize;
  113.     OSErr            err;
  114.  
  115.     if (typeMyWndw != windowDesc->descriptorType)
  116.         return(errAETypeError);
  117.         
  118.     GetRawDataFromDescriptor(windowDesc, (Ptr)&aWindowToken, sizeof(aWindowToken), &actualSize);
  119.  
  120.     err = RevertWindowToken(&aWindowToken);
  121.     
  122.     return(err);
  123. }
  124.  
  125.  
  126. OSErr    RevertDesc(AEDesc* aDesc)
  127. {
  128.     AEDesc        revertDesc = {typeNull, NULL},
  129.                 windowDesc = {typeNull, NULL},
  130.                 itemDesc = {typeNull, NULL};
  131.     long        itemCount,
  132.                 index;
  133.     DescType    theAEKeyword;
  134.     OSErr        err;
  135.     
  136.     if (typeObjectSpecifier == aDesc->descriptorType)
  137.         err = AEResolve(aDesc, kAEIDoMinimum, &revertDesc);
  138.     else
  139.         err = AEDuplicateDesc(aDesc, &revertDesc);
  140.         
  141.     if (noErr != err) goto done;
  142.     
  143.     switch (revertDesc.descriptorType)
  144.     {
  145.         case typeAEList:
  146.             err = AECountItems(&revertDesc, &itemCount);
  147.             if (noErr != err) goto done;
  148.             
  149.             for (index = 1; index <= itemCount; index++)    // Do front back order
  150.             {
  151.                 err = AEGetNthDesc(&revertDesc, index, typeWildCard, &theAEKeyword, &itemDesc);
  152.                 if (noErr != err) goto done;
  153.                 
  154.                 err = RevertDesc(&itemDesc);    // Call recursively
  155.                 if (noErr != err) goto done;
  156.  
  157.                 if (itemDesc.dataHandle)
  158.                     AEDisposeDesc(&itemDesc);
  159.             }
  160.             break;
  161.             
  162.         default:
  163.             err = AECoerceDesc(&revertDesc, typeMyWndw, &windowDesc);
  164.             if (noErr != err) goto done;
  165.             err = RevertWindowDesc(&windowDesc);
  166.     }
  167.     
  168. done:
  169.     if (revertDesc.dataHandle)
  170.         AEDisposeDesc(&revertDesc);
  171.     if (windowDesc.dataHandle)
  172.         AEDisposeDesc(&windowDesc);
  173.     if (itemDesc.dataHandle)
  174.         AEDisposeDesc(&itemDesc);
  175.     
  176.     return(err);
  177. }
  178.